package com.foreknow.dao.impl;

import com.foreknow.dao.CartDao;
import com.foreknow.po.Business;
import com.foreknow.po.Cart;
import com.foreknow.po.Food;
import com.foreknow.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection conn = null;
    private PreparedStatement pst = null;

    @Override
    public List<Cart> listCart(String userId,Integer businessId) throws SQLException {
        StringBuffer sql = new StringBuffer("select c.cartId,c.foodId,c.businessId,c.userId,c.quantity,foodName,foodExplain,foodImg,foodPrice, " +
                " f.remarks as fremarks,b.businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,b.remarks as bremarks\n" +
                " from cart c join food f" +
                " on c.foodId = f.foodId " +
                " join business b " +
                " on f.businessId = b.businessId" +
                " where c.userId = ? ");
        List<Cart> list = new ArrayList<>();
        try{
            if (businessId != null){
                sql.append("and c.businessId = ?");
            }
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql.toString());
            pst.setString(1,userId);
            if (businessId != null){
                pst.setInt(2,businessId);
            }
            ResultSet rs = pst.executeQuery();
            while (rs.next()){
                Cart cart = new Cart();
                Food food = new Food();
                Business business = new Business();
                cart.setCartId(rs.getInt("cartId"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                cart.setUserId(rs.getString("userId"));
                cart.setQuantity(rs.getInt("quantity"));
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("fremarks"));
                food.setBusinessId(rs.getInt("businessId"));
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("bremarks"));
                cart.setFood(food);
                cart.setBusiness(business);
                list.add(cart);
            }
        }finally {
            DBUtil.close(pst);
        }

        return list;
    }


    @Override
    public Integer saveCart(String userId, int foodId, int businessId) throws SQLException {
        String sql = "insert into cart(userId,foodId,businessId,quantity) values (?,?,?,1)";
        int result = 0;
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setString(1,userId);
            pst.setInt(2,foodId);
            pst.setInt(3,businessId);
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public Integer updateCart(String userId, int foodId, int businessId, int quantity) throws SQLException {
        String sql = "update cart set quantity = ? where userId = ? and foodId = ? and businessId = ? ";
        int result = 0;
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setInt(1,quantity);
            pst.setString(2,userId);
            pst.setInt(3,foodId);
            pst.setInt(4,businessId);
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public Integer removeCart(String userId, Integer foodId, Integer businessId) throws SQLException {
        StringBuffer sql = new StringBuffer("delete from cart where userId = ? and businessId = ?");
        int result = 0;

        if (foodId != null){
            sql.append(" and foodId = ?");
        }
        try{
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql.toString());
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            if (foodId != null){
                pst.setInt(3,foodId);
            }
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }

        return result;
    }


    public static void main(String[] args) {
        CartDao cartDao = new CartDaoImpl();
        try {
//            List<Cart> list = cartDao.listCart("12345671111",10001);
//            for (Cart c: list){
//                System.out.println(c.getCartId());
//                System.out.println(c.getFood().getFoodName());
    //        int result = cartDao.saveCart("12345671111",5,10002);

//            int result = cartDao.updateCart("12345671111",5,10001,10);
           int result = cartDao.removeCart("12345671111",null,10002);
            System.out.println(result);
//            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
